/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.lucidhex.viewer.mapimport;


import com.lucidhex.LucidWorld;
import com.lucidhex.hex.LucidHex;
import com.lucidhex.hex.LucidHexType;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

/**
 *
 * @author Wolf
 */
public class MapPicture extends javax.swing.JPanel {
    BufferedImage img;
    private MaptoHexImport imp;
    private MapImportMouseListener iml;
    private double scale = 1.0;
    private Point start = null;
    private Point end = null;
    private LucidWorld world;
    /**
     * Creates new form MapPicture
     */
    public MapPicture() {
        initComponents();

    }
    public void setMapImportFrame(MaptoHexImport imp, LucidWorld world){
        this.imp = imp;
        this.world = world;
        iml = new MapImportMouseListener(this, world);
        this.addMouseListener(iml);
        this.addMouseMotionListener(iml);
    }
    public void setCoordinates(Point start, Point end) {
        this.start = revScaled(start);
        this.end = revScaled(end);
        imp.setCoordinates(this.start, this.end, null, null);
    }
    public void setPreCoordinates(Point start, Point end) {
        this.start = revScaled(start);
        this.end = revScaled(end);
        this.repaint();
    }

    @Override
    public void paint(Graphics g) {
       Graphics2D g2 = (Graphics2D) g;
       if(img != null){
           int r = Math.max(img.getHeight(),img.getWidth());
           int q = Math.min(this.getHeight(), this.getWidth());
           scale = 1.0*q/r;
           g2.drawImage(img, AffineTransform.getScaleInstance(scale, scale), null);
           g2.setColor(Color.BLACK);
           for(Point p : imp.getHexCentersAsPoints()){
               Point scaled = scaled(p);
               g2.drawOval(scaled.x, scaled.y, 2, 2);
           }
           g2.setColor(Color.RED);
           if(start != null && end != null){
               Point start_s = scaled(start);
               Point end_s = scaled(end);
               g2.drawLine(start_s.x, start_s.y, end_s.x, end_s.y);
           }
       }else{
           drawInitalScreen(g, "Bad Image Close Import");
       }
    }
    public void addColor(Point loc, LucidHexType aHexType) {
        Point sc = revScaled(loc);
        try{
            int rbgValue = img.getRGB(sc.x, sc.y);
            imp.setHexImportColor(new Color(rbgValue), aHexType);
        }catch(Exception e){}
    }
    public void drawInitalScreen(Graphics gParent, String text){
        Graphics g = gParent.create();
        g.setColor(getBackground());
        g.fillRect(0,0,getSize().width, getSize().height);
        if(text != null){
            g.setColor(Color.BLACK);
            g.drawString(text, getSize().width/3, getSize().height/3);            
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void setImg(BufferedImage img) {
        this.img = img;
    }

    private Point scaled(Point loc) {
        return new Point((int)(loc.x*scale), (int)(loc.y*scale));
    }
    private Point revScaled(Point loc) {
        return new Point((int)(loc.x/scale), (int)(loc.y/scale));
    }



}
